//
//  SISSRecommendPriceCell.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/12/8.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSRecommendPriceCell.h"

@interface SISSRecommendPriceCell ()<UITextFieldDelegate>

/// leftLabel
@property (weak, nonatomic) UILabel *leftLabel;
/// textField
@property (weak, nonatomic) UITextField *textField;
/// Label
@property (weak, nonatomic) UILabel *unitLabel;
/// recommendPriceButton
@property (weak, nonatomic) UIButton *recommendPriceButton;

/// bottomLine
@property (weak, nonatomic) UIView *bottomLine;

@end
@implementation SISSRecommendPriceCell

- (void)setContractModel:(SISSContractModel *)contractModel {
    [super setContractModel:contractModel];
    
    
    self.textField.text = contractModel.rightStr;
    
    self.leftLabel.hidden = !contractModel.leftStr;
    if ([[contractModel.leftStr substringToIndex:1] isEqualToString:@"*"]) {
        NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:contractModel.leftStr];
        [attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,1)];
        self.leftLabel.attributedText = attributedStr;
    } else {
        self.leftLabel.text = contractModel.leftStr;
    }
    
    self.textField.placeholder = contractModel.placeHolder;
    self.unitLabel.text = contractModel.unit;
    
//    [self.recommendPriceButton setTitle:contractModel.leftTwoStr forState:UIControlStateNormal];
    
}


#pragma mark - UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    self.contractModel.rightStr = textField.text;
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField == self.textField && self.contractModel.firstLimitLength > 0) {
        NSString *str = [NSString stringWithFormat:@"%@%@",textField.text,string];
        if (str.length > self.contractModel.firstLimitLength) {
            [SISSTools showSVTitle:self.contractModel.firstLimitTipStr andTime:1 andColor:YES];
            return NO;
        }
    }
 
    return YES;
}

#pragma mark - 点击事件
- (void)recommendPriceButtonClickAction:(UIButton *)button {
    if ([self.delegate respondsToSelector:@selector(clickRecommendPriceButtonWithCell:)]) {
        [self.delegate clickRecommendPriceButtonWithCell:self];
    }
}



#pragma mark - UI
- (void)initUI {
    [super initUI];
    
    UILabel *leftLabel = [[UILabel alloc] init];
    leftLabel.textColor = MAIN_TEXT_COLOR;
    leftLabel.font = [UIFont systemFontOfSize:16];
    leftLabel.hidden = YES;
    [self.contentView addSubview:leftLabel];
    self.leftLabel = leftLabel;
    
    UITextField *textField = [[UITextField alloc] init];
    textField.textColor = MAIN_TEXT_COLOR;
    textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" " attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:SECONDARY_TEXT_COLOR}];
    textField.placeholder = @"请输入楼层";
    textField.textAlignment = NSTextAlignmentCenter;
    textField.keyboardType = UIKeyboardTypeNumberPad;
    textField.delegate = self;
    [self.contentView addSubview:textField];
    self.textField = textField;
    
    UILabel *unitLabel = [[UILabel alloc] init];
    unitLabel.numberOfLines = 0;
    unitLabel.textColor = MAIN_TEXT_COLOR;
    unitLabel.font = [UIFont systemFontOfSize:16];
    [self.contentView addSubview:unitLabel];
    self.unitLabel = unitLabel;
    
    UIButton *recommendPriceButton = [UIButton buttonWithType:UIButtonTypeCustom];
    recommendPriceButton.layer.cornerRadius = 4;
    recommendPriceButton.layer.masksToBounds = YES;
    recommendPriceButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [recommendPriceButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [recommendPriceButton setBackgroundColor:COLOR(0x54B095)];
    [recommendPriceButton addTarget:self action:@selector(recommendPriceButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:recommendPriceButton];
    self.recommendPriceButton = recommendPriceButton;
    
    UIView *bottomLine = [[UIView alloc] init];
    bottomLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:bottomLine];
    self.bottomLine = bottomLine;
    
}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    CGFloat margin = 8.0;
    CGSize buttonSize = CGSizeMake(54, 28);
    [self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView).offset(1.5 * margin);
        make.left.equalTo(weakSelf.contentView).offset(2 * margin);
    }];
    
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView).offset(20 * margin);
        make.centerY.equalTo(weakSelf.contentView);
    }];
    
    [self.unitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.textField);
        make.right.equalTo(weakSelf.contentView).offset(-0.75 * margin);
//        make.right.equalTo(weakSelf.recommendPriceButton.mas_left).offset(-0.5 * margin);
    }];
    self.recommendPriceButton.hidden = YES;
//    [self.recommendPriceButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(weakSelf.contentView).offset(-0.75 * margin);
//        make.centerY.equalTo(weakSelf.contentView);
//        make.size.mas_equalTo(buttonSize);
//    }];
    
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.leftLabel.mas_bottom).offset(1.5 * margin);
        make.left.right.bottom.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(1);
    }];
}



@end
